"""
从文件解析数据: 提取登录信息进行测试。
"""
from seleniumbase import BaseCase
import pandas as pd

class ParseTestCase(BaseCase):

    def 获取不同角色的账号(self):
        """
        根据角色名返回对应的账号密码
        """
        dic = {}
        表格数据 = pd.read_excel("用户.xlsx", sheet_name=0)
        dic["管理权限"] = {"账户": str(表格数据.values[0][1]), "密码": str(表格数据.values[0][2])}
        dic["运营主管"] = {"账户": str(表格数据.values[1][1]), "密码": str(表格数据.values[1][2])}
        dic["运营"] = {"账户": str(表格数据.values[2][1]), "密码": str(表格数据.values[2][2])}
        dic["客服主管"] = {"账户": str(表格数据.values[3][1]), "密码": str(表格数据.values[3][2])}
        dic["客服"] = {"账户": str(表格数据.values[4][1]), "密码": str(表格数据.values[4][2])}
        dic["远程专家客服"] = {"账户": str(表格数据.values[5][1]), "密码": str(表格数据.values[5][2])}
        print(dic["管理权限"]["账户"])
        print(dic["管理权限"]["密码"])
        return dic


    def setUp(self):
        super(ParseTestCase, self).setUp()

    def get_login_credentials(self, user_type):
        # 第一种方法：从文件中解析数据
        with open("qa_login_example.txt") as f:
            file_lines = [line.rstrip() for line in f]
        for line in file_lines:
            line_items = line.split(",")
            if line_items[0] == user_type:
                return line_items[1], line_items[2]

    def get_all_login_credentials(self):
        # 第二种方法：从文件中解析数据成字典，运维、客服、主管角色
        keys = {}
        with open("staging_login_example.txt") as f:
            file_lines = [line.rstrip() for line in f]
        for line in file_lines:
            line_items = line.split(",")
            if line_items[0] == "admin":
                keys["admin"] = {
                    "username": line_items[1],
                    "password": line_items[2],
                }
            if line_items[0] == "employee":
                keys["employee"] = {
                    "username": line_items[1],
                    "password": line_items[2],
                }
            if line_items[0] == "customer":
                keys["customer"] = {
                    "username": line_items[1],
                    "password": line_items[2],
                }
        return keys


class ParseTests(ParseTestCase):
    def test_get_login_credentials(self):
        print("\n通过解析配置文件获取登录信息的示例 1:")
        print("")
        username, password = self.get_login_credentials("admin")
        print("Getting Admin User login data:")
        print("Username: %s" % username)
        print("Password: %s" % password)

        print("\n通过解析配置文件获取登录信息的示例 2：")
        print("")
        keys = self.get_all_login_credentials()
        print("Getting Customer login data:")
        print("Username: %s" % keys["customer"]["username"])
        print("Password: %s" % keys["customer"]["password"])


if __name__ == '__main__':
    a = ParseTests()
    a.获取不同角色的账号()